<?php
	
	namespace Aspic\View;
	use Aspic as A;

	/**
	* This class is used to generate page numbers under elements like : << < 1 - 2 - 3 > >>
	* @todo Change urlFormat arg to urlFunction that will takes page as first arg and should return url to this page
	*/
	class PageListNavigation {
		
		use A\Preset\Std {
			__construct as initStdTrait;
		}
		
		protected $_urlFormat;
		protected $_currPage;
		protected $_itemsPerPage;
		protected $_totalItems;
		
		public function __construct($currPage, $itemsPerPage, $totalItems, $urlFormat, array $options = array()) {
			$this->initStdTrait(array(), true);
			
			$this->_currPage = $currPage;
			$this->_itemsPerPage = $itemsPerPage;
			$this->_totalItems = $totalItems;
			$this->_urlFormat = $urlFormat; // localhost/site/usersList.php?page=?
			
			$this->setOptions($options);
			$this->_setDefaultOptions();
		}
		
		public function setOptions($opts) {
			$this->setVars($opts);
		}
		
		protected function _setDefaultOptions() {
			$this->pagesBeforeCurr = 4;
			$this->pagesAfterCurr = 5;
			$this->pagesNumberSeparator = ' ';
			
			$this->previousPageShortcutCts = '<';
			$this->nextPageShortcutCts = '>';
			
			$this->firstPageShortcutCts = '<<';
			$this->lastPageShortcutCts = '>>';
		}
		
		protected function _getNbPages() {
			$nbPages = ceil($this->_totalItems / $this->_itemsPerPage);
			
			return $nbPages;
		}
		
		protected function _getNavParts() {
				if($this->firstPageShortcutCts) {
				if($this->_currPage > 1) {
					$firstPageShortcutCts = '<a href="'.A\UString::fromArgs($this->_urlFormat, 1).'">'.$this->firstPageShortcutCts.'</a>';
				}
				else {
					$firstPageShortcutCts = $this->firstPageShortcutCts;
				}
			}
			else $firstPageShortcutCts = '';
			
			if($this->lastPageShortcutCts) {
				if($this->_currPage < $this->_getNbPages()) {
					$lastPageShortcutCts = '<a href="'.A\UString::fromArgs($this->_urlFormat, 1).'">'.$this->lastPageShortcutCts.'</a>';
				}
				else {
					$lastPageShortcutCts = $this->lastPageShortcutCts;
				}
			}
			else $lastPageShortcutCts = '';

			// ----------------
			
			if($this->previousPageShortcutCts) {
				if($this->_currPage > 1) {
					$previousPageShortcutCts = '<a href="'.A\UString::fromArgs($this->_urlFormat, $this->_currPage - 1).'">'.$this->previousPageShortcutCts.'</a>';
				}
				else {
					$previousPageShortcutCts = $this->previousPageShortcutCts;
				}
			}
			else $previousPageShortcutCts = '';
			
			if($this->nextPageShortcutCts) {
				if($this->_currPage < $this->_getNbPages()) {
					$nextPageShortcutCts = '<a href="'.A\UString::fromArgs($this->_urlFormat, $this->_currPage + 1).'">'.$this->nextPageShortcutCts.'</a>';
				}
				else {
					$nextPageShortcutCts = $this->nextPageShortcutCts;
				}
			}
			else $nextPageShortcutCts = '';

			// ----------------
			
			$pagesNumbersArr = array();
			
			$startPage = ($this->pagesBeforeCurr == null)?1:$this->pagesBeforeCurr;
			
			for ($page = $startPage ; $page < $this->_currPage ; ++$page) {
				$pagesNumbersArr[] = '<a href="'.A\UString::fromArgs($this->_urlFormat, $page).'">'.$page.'</a>';
			}
			
			$pagesNumbersArr[] = $this->_currPage;
			
			$endPage = ($this->pagesAfterCurr == null)?$this->_getNbPages:$this->_currPage + $this->pagesAfterCurr;
			
			for (++$page; $page < $this->_getNbPages() AND $page <= $endPage; ++$page) {
				$pagesNumbersArr[] = '<a href="'.A\UString::fromArgs($this->_urlFormat, $page).'">'.$page.'</a>';
			}
			
			$pagesNumbersStr = implode($this->pagesNumberSeparator, $pagesNumbersArr);
			
			$parts = array(
				$firstPageShortcutCts, 
				$previousPageShortcutCts, 
				$pagesNumbersStr, 
				$nextPageShortcutCts, 
				$lastPageShortcutCts
			);
			
			return $parts;
		}
		
		public function out() {
			list(
				$firstPageShortcutCts, 
				$previousPageShortcutCts, 
				$pagesNumbersStr, 
				$nextPageShortcutCts, 
				$lastPageShortcutCts
			) = $this->_getNavParts();
			
			$out = implode(' ', array(
					$firstPageShortcutCts, 
					$previousPageShortcutCts, 
					$pagesNumbersStr, 
					$nextPageShortcutCts, 
					$lastPageShortcutCts
				)
			);
			
			return $out;
		}
		
		public function __toString() {
			return $this->out();
		}
	}
	
?>